package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService ;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService ;

    @Autowired
    private GraphInfoService graphInfoService ;

    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {

        Long id = apartmentSubmitVo.getId();
        if(id!=null){ // 如果id不为null，则说明执行的是更新操作，因此需要先删除各种旧关系，然后在每一个关系表中新增新的关系
            // 1) 删除旧的配套关系
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
            // 2) 删除旧的标签关系
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
            // 3) 删除旧的杂费值关系
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
            // 4) 删除旧图片列表
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.APARTMENT)) ;
        }
        
        //1. 保存公寓基本信息
        apartmentInfoService.saveOrUpdate(apartmentSubmitVo);

        //2. 保存配套关系信息
        List<ApartmentFacility> apartmentFacilityList = apartmentSubmitVo.getFacilityInfoIds().stream()
                .map(facilityId -> ApartmentFacility.builder().apartmentId(id).facilityId(facilityId).build())
                .collect(Collectors.toList());
        apartmentFacilityService.saveBatch(apartmentFacilityList);

        //3. 保存标签关系信息
        List<ApartmentLabel> labelList = apartmentSubmitVo.getLabelIds().stream()
                .map(labelId -> ApartmentLabel.builder().apartmentId(id).labelId(labelId).build())
                .collect(Collectors.toList());
        apartmentLabelService.saveBatch(labelList);

        //4. 保存杂费值关系信息
        List<ApartmentFeeValue> apartmentFeeValueList = apartmentSubmitVo.getFeeValueIds().stream()
                .map(feeValueId -> ApartmentFeeValue.builder().apartmentId(id).feeValueId(feeValueId).build())
                .collect(Collectors.toList());
        apartmentFeeValueService.saveBatch(apartmentFeeValueList);

        //5. 保存公寓图片列表信息
        List<GraphInfo> graphInfoList = apartmentSubmitVo.getGraphVoList().stream()
                .map(graphVo -> {
                    GraphInfo graphInfo = new GraphInfo();
                    BeanUtils.copyProperties(graphVo, graphInfo);
                    graphInfo.setItemId(id);
                    graphInfo.setItemType(ItemType.APARTMENT);
                    return graphInfo;
                }).collect(Collectors.toList());
        graphInfoService.saveBatch(graphInfoList);

        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        // 目前我们学习了这几种VO： Vo-普通的视图对象； QueryVo-封装查询条件的视图对象； ItemVo-封装列表项的视图对象； SubmitVo-封装表单提交的视图对象

        IPage page= Page.of(current,size) ;
        IPage<ApartmentItemVo> apartmentItemVoIPage = apartmentInfoService.pageItem(page,queryVo);
        return Result.ok(apartmentItemVoIPage);
    }

    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        // 通过此方法我们有新学习了一个VO：  XXXDetailVo ， 一般用于显示详情

        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getDetailById(id);
        return Result.ok(apartmentDetailVo);
    }

    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        apartmentInfoService.removeApartmentById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {
        apartmentInfoService.update(new LambdaUpdateWrapper<ApartmentInfo>().set(ApartmentInfo::getIsRelease,status).eq(ApartmentInfo::getId,id));
        return Result.ok();
    }

    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        List<ApartmentInfo> apartmentInfoList = apartmentInfoService.list(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getDistrictId, id));
        return Result.ok(apartmentInfoList);
    }
}














